<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FileController extends Controller
{
    function upload(Request $request){
        if($request->hasFile('Filedata') and $request->file('Filedata')->isValid()){
            $result = array();
            //判断文件类型
            $allow = array('image/jpeg', 'image/png', 'image/gif');
            $mine = $request->file('Filedata')->getMimeType();
            if(!in_array($mine,$allow)){
                $result['status'] = 0;
                $result['info'] = '文件类型错误，只能上传图片！';
                return $result;
            }
            //判断文件大小
            $max_size = 1024*3*1024;
            $size = $request->file('Filedata')->getClientSize();
            if($size > $max_size){
                $result['status'] = 0;
                $result['info'] = '文件大小不能超过3M';
                return $result;
            }
            //返回文件名
            $time =  date("Y_m");
            $path = $request->file('Filedata')->store('public/images/' . $time);
            $filePath_local = storage_path('app/').$path;

            list($ret, $err) = qiniu_upload($filePath_local,0);

            if ($err !== null) {
                //var_dump($err);
                $result['status'] = 0;
                $result['info'] = '七牛问题！';
                return $result;
            } else {
                //var_dump($ret);
                $result['status'] = 1;
                //$result['info'] = '/storage' . ltrim($path, 'public');
                $result['info'] = env('QINIU_URL').$ret['key'];
                return $result;
            }
        }
    }


}
